import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

public class ProResultSet{
    public static void main(String[] args){
        
        try{
            //装载Driver类
            Class.forName("com.mysql.jdbc.Driver");
            
            //连接数据库
            String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
            String username = "root";
            String password = "";
            Connection conn = DriverManager.getConnection(url, username, password);
            
            //查询语句，获得结果集
            String dbName = "animals";
            String sql = "select * from " + dbName;
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            
            //ResultSet是可迭代的
            while(rs.next()){
                // String name = rs.getString("name");
                //getString(int)更高效
                String name = rs.getString(2);
                System.out.println(name);
            }
                        
        }catch(ClassNotFoundException e){
            System.out.println("没找找到加载类");
            e.printStackTrace();
        }catch(SQLException e){
            System.out.println("数据库连接失败");
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }
        
    }
}